from ctypes import *
import os
# script_dir = os.path.dirname(__file__)
# 构建配置文件的路径
# dll_path = os.path.join(script_dir, r"VerificationCode\1.0\bin\x64\Release\VerificationCode.dll")

class AgEncode():
    def __init__(self):
        self.dll_path = os.path.dirname(__file__)+b"\VerificationCode.dll".decode("utf-8")
        print(self.dll_path)
        try:
            self._dll = windll.LoadLibrary(self.dll_path)
            print("DLL 加载成功。")
        except OSError as e:
            print(f"无法加载 DLL: {e}")


        # agVCEncode 函数接受一个指向 unsigned char 的指针，并返回 None
        self._dll.agVCEncode.argtypes = [POINTER(c_ubyte)]
        self._dll.agVCEncode.restype = None

    def ag_encode(self, data_list):
        
        # 创建一个包含四个字节的 ctypes 缓冲区
        data = create_string_buffer(bytes(data_list))  
        
        # 将缓冲区转换为 POINTER(c_ubyte) 类型
        data_pointer = cast(data, POINTER(c_ubyte))


        # 调用函数
        self._dll.agVCEncode(data_pointer)

        print(f"加密后的数据: {data.raw}")
        return data.raw

if __name__ == '__main__':
    a = AgEncode()
    b = a.ag_encode([0x11, 0x22, 0x56, 0xAA])
    print(hex(b[0]))
    print(hex(b[1]))
    print(hex(b[2]))
    print(hex(b[3]))